count the triplets in java